<?php
    require("../models/webconfig.php");
    require("../models/class.connection.php");
    require("../models/entities/class.ProductEntity.php");
    require("../models/repositories/class.ProductRepository.php");
    include("../library/library.pager.php");
    $productRepository = new ProductRepository();
    $productName = "";
    $arrColor = array();
    $arrCategoryID = array();
    if(isset($_GET['category_IDs']))
    {
        if($_GET['category_IDs'] != "-Any-")
        {
            $arrCategoryID = explode('|', $_GET['category_IDs']);
        }
    }
    if(isset($_GET['colors']))
    {
        if($_GET['colors'] != "-Any-")
        {
            $arrColor = explode('|', $_GET['colors']);
        }
    }			
	if(isset($_GET['productName']))
	{
        if($_GET['productName'] != "-Any-")
        {
            $productName = $_GET['productName'];
        }
    }
    $curPage = 1;
    if(isset($_GET['page']))
    {
        if(is_numeric($_GET['page']))
            $curPage = $_GET['page'];
    }
    $totalItems = 0;
    $results = $productRepository->SearchProductPager($curPage, 20, $arrCategoryID, $arrColor, $productName, $totalItems);
    $generateLink = GenerateLink($totalItems, 5, 20, $curPage);
?>
<div class="loading"></div>
<div class="pagination pagination-left">
    <div class="results">
        <span>
            Kết quả từ <?php echo (($curPage - 1)*20 + (count($results) > 0 ? 1 : 0));?> - <?php echo (($curPage - 1)*20 + count($results));?> của <?php echo $totalItems ?>
        </span>
    </div>
    <ul class="pager"><?php echo $generateLink ?></ul>
</div>
<div class="clearall"></div>
<table>
    <tr>
        <td>STT</td>
        <td>Mã SP</td>
        <td>Tên SP</td>
        <td>Màu sắc</td>
        <td>Giá</td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <?php
        for($i = 0; $i < count($results); $i++){
    ?>
    <tr>
        <td><?php echo ($i + 1); ?></td>
        <td><?php echo $results[$i]->Code_s; ?></td>
        <td>
            <a class="detail" href="#" code="<?php echo $results[$i]->Code_s ?>" onclick="return false;">
                <?php
                    $arr = explode('|', $results[$i]->ImageSrc_s);                                            
                ?>
                <img style="max-width: 60px; max-height: 150px;" src="../content/images/Products/<?php echo $results[$i]->Code_s;?>/<?php echo $arr[0];?>" />
            </a>
            <br />
            <a class="detail" href="" code="<?php echo $results[$i]->Code_s ?>" onclick="return false;"><?php echo $results[$i]->Name_s; ?></a>
        </td>
        <td><?php echo $results[$i]->Color_s; ?></td>
        <td><?php echo number_format($results[$i]->Price_f); ?></td>
        <td><a class="detail" href="" code="<?php echo $results[$i]->Code_s ?>" onclick="return false;">Chi tiết</a></td>
        <td><a href="EditProduct.php?code=<?php echo $results[$i]->Code_s; ?>">Sửa</a></td>
        <td><a href="#" onclick="return false;" class="deleteProduct" code="<?php echo $results[$i]->Code_s; ?>">Xóa</a></td>
    </tr>
    <?php
        }
    ?>
</table>